C++ Primier Plus Generic Algorithms

find algorithm: auto result = find(vec.cbegin(), vec.cend(), val);
It returns an iterator to the first element that is equal to that value.
use the library begin and end functions to pass a pointer to the first and one past the last elements in ia.

1
2
3
4
5
6
7
int ia[] = {27, 210, 12, 47, 109, 83};
int val = 83;
//int* result = find(begin(ia), end(ia), val);
// count(v.cbegin(), v.cend(), value)
// int sum = accumulate(vec.cbegin(), vec.cend(), 0);
The accumulate function takes three arguments. The first two specify a
range of elements to sum. The third is an initial value for the sum.

equal(roster1.cbegin(), roster1.cend(), roster2.cbegin());
fill(vec.begin(), vec.end(), value);
fill(vec.begin(), vec.begin() + vec.size()/2, value);
fill_n(dest, n, val)
Algorithms that write to a destination iterator assume the destination is large enough to hold the number of elements being written.
back_inserter takes a reference to a container and returns an insert iterator bound to that container. When we assign through that iterator, the assignment calls push_back to add an element with the given value to the container.

1
2
3
vector<int> vec; // empty vector
auto it = back_inserter(vec); // assigning through it adds elements to vec
*it = 42; // vec now has one element with value 42

auto ret = copy(begin(a1), end(a1), a2);
The value returned by copy is the (incremented) value of its destination iterator. That is, ret will point just past the last element copied into a2.
replace(ilst.begin(), ilst.end(), 0, 42);
This call replaces all instances of 0 by 42. If we want to leave the original sequence unchanged, we can call replace_copy.
back_inserter is a insert iterator, what iterator adaptor that generates an iterator that uses a container operation to add elements to a given container.
the algorithms don’t change the size, but the iterator can change it by using the container operation.

Eliminating Duplicates

1
2
3
4
5
6
7
8
9
10
void elimDups(vector<string> &words)
{
// sort words alphabetically so we can find the duplicates
sort(words.begin(), words.end());
// unique reorders the input range so that each word appears once in the
// front portion of the range and returns an iterator one past the unique range
auto end_unique = unique(words.begin(), words.end());
// erase uses a vector operation to remove the nonunique elements
words.erase(end_unique, words.end());
}

  1. The library algorithms operate on iterators, not containers. Therefore, an
    algorithm cannot (directly) add or remove elements.
  2. To actually remove the unused elements, we must use a container operation

A lambda expression represents a callable unit of code. It can be thought of as an unnamed, inline function.

lambda

http://www.cnblogs.com/knowledgesea/p/3163725.html
[capture list] (parameter list) -> return type { function body }
The capture list directs the lambda to include information needed to access those variables within the lambda itself.
The capture list is used for local nonstatic variables only; lambdas can use local statics and variables declared outside the function directly.
When we capture a variable by reference, we must ensure that the variable exists at the time that the lambda executes.
By default, a lambda may not change the value of a variable that it copies by value.
be able to change the value of a captured variable, we must follow the parameter list with the keyword mutable.

Insert Iterators
• back_inserter (§ 10.2.2, p. 382) creates an iterator that uses push_back.
• front_inserter creates an iterator that uses push_front.
• inserter creates an iterator that uses insert. This function takes a second argument, which must be an iterator into the given container. Elements are inserted ahead of the element denoted by the given iterator.

it is not possible to create a reverse iterator from a forward_list or a stream iterator.

List the five iterator categories and the operations that each supports.
• Input iterators : ==, !=, ++, , ->
• Output iterators : ++,

• Forward iterators : ==, !=, ++, , ->
• Bidirectional iterators : ==, !=, ++, –,
, ->
• Random-access iterators : ==, !=, <, <=, >, >=, ++, –, +, +=, -, -=, -(two iterators), , ->, iter[n] == (iter + n)

The list member versions should be used in preference to the generic algorithms for lists and forward_lists.
a crucially important difference between the list-specific and the generic versions is that the list versions change the underlying container.